//
//  ViewController.m
//  自定义布局
//
//  Created by 耐克了解了 on 17/4/17.
//  Copyright © 2017年 耐克了解了. All rights reserved.
//

#import "ViewController.h"
#import "TWWaterLayout.h"
#import "TWPhotoCell.h"
#import "MJExtension.h"
#import "MJRefresh.h"
#import "XMGShop.h"
#import "XMGShopCell.h"

static NSString *cellID = @"shop";

@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,TWWaterLayoutDelegate>

@property (nonatomic,strong) NSMutableArray *shops;

@property (nonatomic,strong) UICollectionView *collectionView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    [self setupLayout];
    
    [self setupREfresh];
}

- (NSMutableArray *)shops
{
    if (!_shops) {
        _shops = [NSMutableArray array];
    }
    return _shops;
}

- (void)setupREfresh{
    self.collectionView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadShops)];
    [self.collectionView .header beginRefreshing];
    
    self.collectionView.footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreShops)];
    self.collectionView.footer.hidden = YES;
}

- (void)loadShops{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSArray *shops = [XMGShop objectArrayWithFilename:@"1.plist"];
        [self.shops removeAllObjects];
        [self.shops addObjectsFromArray:shops];
        [self.collectionView reloadData];
        [self.collectionView.header endRefreshing];
    });
}

- (void)loadMoreShops{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSArray *shops = [XMGShop objectArrayWithFile:@"1.plist"];
      
        [self.shops addObjectsFromArray:shops];
        [self.collectionView reloadData];
        [self.collectionView.footer endRefreshing];
    });
}


- (void)setupLayout
{
    
    TWWaterLayout *layout = [[TWWaterLayout  alloc]init];
    layout.delegate = self;
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    [self.view addSubview:collectionView];
   
    collectionView.backgroundColor = [UIColor whiteColor];
    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([XMGShopCell class]) bundle:nil] forCellWithReuseIdentifier:cellID];//registerClass:[XMGShopCell class] forCellWithReuseIdentifier:cellID];
    collectionView.dataSource = self;
    collectionView.delegate = self;
   self.collectionView = collectionView;
}

#pragma mark - TWWaterFlowlayoutDelegate
- (CGFloat)waterFlowLayout:(TWWaterLayout *)waterLayout heighForItemIndex:(NSInteger)index itemWidth:(CGFloat)itemWidth
{
    XMGShop *shop = self.shops[index];
    return itemWidth *shop.h /shop.w;
}

-(CGFloat)rowMarginInWaterFlowlayout:(TWWaterLayout *)waterFlowLayout
{
    return 10;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    self.collectionView.footer.hidden = self.shops.count == 0;
    return self.shops.count ;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XMGShopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
 
    //cell.imageName = [NSString stringWithFormat:@"%zd",indexPath.item +1];
    cell.shop = self.shops[indexPath.item];
//    NSInteger tag = 10;
//    cell.backgroundColor = [UIColor orangeColor];
//    UILabel *label = [cell.contentView viewWithTag:tag];
//    if (label == nil) {
//        label = [[UILabel alloc]init];
//        label.tag = 10;
////        label.backgroundColor = [UIColor orangeColor];
//        [cell.contentView addSubview:label];
//    }
//    label.text = [NSString stringWithFormat:@"%zd",indexPath.item];
//    
//    [label sizeToFit];
  return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
